import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../config/color_config.dart';
import '../../config/string_config.dart';
import '../../utils/screen_util.dart';

class SrcCodeDialog extends StatefulWidget {
  const SrcCodeDialog({Key? key}) : super(key: key);

  @override
  State<SrcCodeDialog> createState() => _SrcCodeDialogState();
}

class _SrcCodeDialogState extends State<SrcCodeDialog> {
  final TextEditingController _controller = TextEditingController();
  // 定义与原生通信的MethodChannel
  static const platform = MethodChannel('com.ubix.kiosoft2/flutter_channel');
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  bool _isValidRoomCode(String code) {
    // 验证是否为7位字母数字组合
    return RegExp(r'^[a-zA-Z0-9]{7}$').hasMatch(code);
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(14),
      ),
      child: Container(
        width: double.infinity,
        padding: EdgeInsets.zero,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const SizedBox(height: 20),
            const Text(
              StringConfig.enterlocation_id,
              style: TextStyle(
                fontSize: 17,
                fontWeight: FontWeight.w600,
                color: Colors.black,
              ),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(24, 3, 24, 24),
              child: const Text(
                StringConfig.enterlocation_id_msg,
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 13,
                  color: ColorConfig.dialogDescriptionColor,
                  height: 1.3,
                ),
              ),
            ),
            Padding(
              padding:  EdgeInsets.symmetric(horizontal: ScreenUtil.setHeight(12)),
              child: SizedBox(
                height: ScreenUtil.setHeight(35),
                child: TextField(
                  controller: _controller,
                  textAlign: TextAlign.left,
                  maxLength: 7,
                  style: const TextStyle(
                    fontSize: 16,
                    color: Colors.black,
                  ),
                  decoration: InputDecoration(
                    counterText: '',
                    contentPadding:  EdgeInsets.symmetric(
                      horizontal: ScreenUtil.setHeight(12),
                      vertical: ScreenUtil.setWidth(12),
                    ),
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(6),
                      borderSide: const BorderSide(color: ColorConfig.dialogBorderColor),
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(6),
                      borderSide: const BorderSide(color: ColorConfig.dialogBorderColor),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(6),
                      borderSide: const BorderSide(color: ColorConfig.dialogButtonColor),
                    ),
                  ),
                  inputFormatters: [
                    // 只允许输入字母和数字
                    FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z0-9]')),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            Container(
              decoration: const BoxDecoration(
                border: Border(
                  top: BorderSide(
                    color: ColorConfig.dialogBorderColor,
                    width: 0.5,
                  ),
                ),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Expanded(
                    child: InkWell(
                      onTap: () => Navigator.of(context).pop(),
                      child: Container(
                        height: 44,
                        alignment: Alignment.center,
                        decoration: const BoxDecoration(
                          border: Border(
                            right: BorderSide(
                              color: ColorConfig.dialogBorderColor,
                              width: 0.5,
                            ),
                          ),
                        ),
                        child: const Text(
                          StringConfig.tip_dialog_cancel,
                          style: TextStyle(
                            color: ColorConfig.dialogButtonColor,
                            fontSize: 17,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                    child: InkWell(
                      onTap: () {
                        print("点击confirm");
                        platform.invokeMethod("logd",{"message":"12344556"});
                        if (_isValidRoomCode(_controller.text)) {
                          Navigator.of(context).pop(_controller.text);
                        }
                      },
                      child: Container(
                        height: 44,
                        alignment: Alignment.center,
                        child: const Text(
                          StringConfig.tip_dialog_confirm,
                          style: TextStyle(
                            color: ColorConfig.dialogButtonColor,
                            fontSize: 17,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
